package com.uchiha.nova.mainService.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.model.dto.TenantDeptDTO;
import com.uchiha.nova.core.model.entity.TenantCompanyEntity;
import com.uchiha.nova.core.model.entity.TenantDeptEntity;
import com.uchiha.nova.core.model.query.TenantDeptQuery;
import com.uchiha.nova.core.model.vo.TenantDeptVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.DictBizVO;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.CascaderUtil;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.service.ITenantDeptService;
import com.uchiha.nova.mainService.wrapper.TenantDeptWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 组织机构表  控制器
 *
 * @author Uchihashiamu
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tenantDept")
@Tag(name = "组织机构表 ")
@Slf4j
public class TenantDeptController extends BaseController {

  @Resource
  ITenantDeptService tenantDeptService;
  	/**
	 * 详情
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_GETONE)
	@Operation(summary = "详情")
	public R<TenantDeptVO> getOne(@RequestBody TenantDeptDTO tenantDeptDTO) {
        TenantDeptEntity entity = new TenantDeptEntity();
        BeanUtil.copyProperties(tenantDeptDTO, entity);
		TenantDeptEntity detail = tenantDeptService.getOne(Condition.getQueryWrapper(entity));
		return R.data(TenantDeptWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping(NovaCRUDMethodConstant.NOVA_WEB_PAGE)
  @Operation(summary = "分页查询")
	public R getPage(TenantDeptQuery query, QueryContext queryContext) {
		IPage<TenantDeptEntity> pages = tenantDeptService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext,TenantDeptEntity.class));
		return R.data(TenantDeptWrapper.build().pageVO(pages));
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVE)
	@Operation(summary = "新增")
	public R save(@RequestBody TenantDeptDTO tenantDeptDTO) {
        TenantDeptEntity entity = new TenantDeptEntity();

        BeanUtil.copyProperties(tenantDeptDTO, entity);
		if(ObjectUtil.isEmpty(entity.getLevel())){
			entity.setLevel(1);
		}
		if(ObjectUtil.isEmpty(entity.getParentId())){
			entity.setParentId(0L);
		}
		return R.status(tenantDeptService.save(entity));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_UPDATE)
	@Operation(summary = "修改")
	public R update(@RequestBody TenantDeptDTO tenantDeptDTO) {
        TenantDeptEntity entity = new TenantDeptEntity();
        BeanUtil.copyProperties(tenantDeptDTO, entity);
		return R.status(tenantDeptService.updateById(entity));
	}

	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVEORUPDATE)
	@Operation(summary = "新增或修改")
	public R saveOrUpdate(@RequestBody TenantDeptDTO tenantDeptDTO) {
        TenantDeptEntity entity = new TenantDeptEntity();
        BeanUtil.copyProperties(tenantDeptDTO, entity);
		return R.status(tenantDeptService.saveOrUpdate(entity));
	}


	/**
	 * 删除 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_WEB_REMOVE)
	@Operation(summary = "逻辑删除")
	public R remove(@RequestParam String ids) {
		return R.status(tenantDeptService.deleteLogic(Func.toLongList(ids)));
	}
  
    /**
	 * 物理删除，不可恢复
	 */
   @Operation(summary = "物理删除")
   @DeleteMapping(NovaCRUDMethodConstant.NOVA_WEB_DELETE)
   public R delete(@RequestParam String ids){
     return R.status(tenantDeptService.deletePhysics(Func.toLongList(ids)));
   }

	@GetMapping("/getTree")
	public R getTree() {
		List<TenantDeptEntity> list = tenantDeptService.list();
		List<DictBizVO> all = list.stream().map(item -> {
			DictBizVO dictBizVO = new DictBizVO();
			dictBizVO.setId(item.getId());
			dictBizVO.setParentId(item.getParentId());
			dictBizVO.setLabel(item.getName());
			dictBizVO.setValue(item.getId().toString());
			return dictBizVO;
		}).collect(Collectors.toList());
		List<DictBizVO> listLevelTop = all.stream().filter(item ->item.getParentId().compareTo(0L)!=0).map(item -> {
			item.setChildren(CascaderUtil.getChildren(item, all));
			return item;
		}).collect(Collectors.toList());
		return R.data(listLevelTop);
	}

}
